// 打字机特效

let textArr = [
    "分享博客",
    "分享你的生活"
]
const textEl = $("#typeWriter")

let index = 0;
let charIndex = 0;
let delta = 500;

let start = null
let isDeleting = false;
function type(time){
    window.requestAnimationFrame(type)
    if(!start) start = time;
    let progress = time -start;

    if (progress > delta) {
        let text = textArr[index]
        if (!isDeleting) {
            textEl.html(text.slice(0, ++charIndex))
            delta = 500 - Math.random() * 400
        } else {
            textEl.html(text.slice(0, charIndex--))
        }

        start = time;

        if (charIndex === text.length) {
            isDeleting = true;
            delta = 200
            start = time + 1200
        }

        if (charIndex < 0) {
            isDeleting = false
            start = time + 200;
            index = ++index % textArr.length
        }
    }
}

window.requestAnimationFrame(type)